package com.fenghuajueli.idiomppp.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.ScreenUtils;
import com.fenghuajueli.idiomppp.R;
import com.fenghuajueli.idiomppp.entity.IdiomPinEntity;
import com.fenghuajueli.libbasecoreui.listener.OnListClickListener;


import java.util.List;

/**
 * 需要填充的文字数据适配器
 */
public class IdiomFillTextGridAdapter extends BaseAdapter {

    private List<IdiomPinEntity> idiomPinEntityList;
    private Context mContext;
    private LayoutInflater mInflater;
    private int itemWidth;
    private OnListClickListener<IdiomPinEntity> onListClickListener;


    public IdiomFillTextGridAdapter(Context context, int column, List<IdiomPinEntity> idiomPinEntityList) {
        this.idiomPinEntityList = idiomPinEntityList;
        this.mInflater = LayoutInflater.from(context);
        this.mContext = context;
        itemWidth = (ScreenUtils.getScreenWidth() - ConvertUtils.dp2px((column - 1) * 10) - ConvertUtils.dp2px(36)) / column;
    }

    public void refreshData(List<IdiomPinEntity> idiomPinEntityList, int column) {
        this.idiomPinEntityList = idiomPinEntityList;
        itemWidth = (ScreenUtils.getScreenWidth() - ConvertUtils.dp2px((column - 1) * 10) - ConvertUtils.dp2px(36)) / column;
        notifyDataSetChanged();
    }

    public void setOnListClickListener(OnListClickListener<IdiomPinEntity> onListClickListener) {
        this.onListClickListener = onListClickListener;
    }

    @Override
    public int getCount() {
        return idiomPinEntityList.size();
    }

    @Override
    public Object getItem(int i) {
        return idiomPinEntityList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.idiom_fill_text_item_layout, null);
            holder.idiomText = convertView.findViewById(R.id.idiomText);
            ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(itemWidth, itemWidth);
            convertView.setLayoutParams(layoutParams);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        IdiomPinEntity idiomPinEntity = idiomPinEntityList.get(position);
        if (idiomPinEntity.getBottomFill()) {
            holder.idiomText.setVisibility(View.GONE);
        } else {
            holder.idiomText.setVisibility(View.VISIBLE);
        }
        holder.idiomText.setText(idiomPinEntity.getWord());
        holder.idiomText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                if (!QuickClickUtils.isFastClick()) {
//                    return;
//                }
                idiomPinEntity.setBottomFill(true);
                notifyDataSetChanged();
                onListClickListener.itemClick(position, idiomPinEntity);
            }
        });
        return convertView;
    }

    final class ViewHolder {
        TextView idiomText;
    }

    /**
     * 点击填充过得字时  取消填充到列表，恢复到底部可选的字列表
     *
     * @param text
     */
    public void recoveryItem(String text) {
        for (int i = 0; i < idiomPinEntityList.size(); i++) {
            IdiomPinEntity idiomPinEntity = idiomPinEntityList.get(i);
            if (idiomPinEntity.getWord().equals(text)) {
                idiomPinEntity.setBottomFill(false);
            }
        }
        notifyDataSetChanged();
    }

}
